package com.pvj.addresslistdemo.widget.widget;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pvj.addresslistdemo.MyLocalParticleAbility;
import com.pvj.addresslistdemo.widget.controller.FormController;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;
import ohos.utils.zson.ZSONReader;

import java.io.StringReader;
import java.util.Iterator;
import java.util.function.Consumer;


public class WidgetImpl extends FormController {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, WidgetImpl.class.getName());
    private static final int DIMENSION_1X2 = 1;

    public WidgetImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
    }

    @Override
    public ProviderFormInfo bindFormData(long formId) {
        HiLog.info(TAG, "bind form data");
        ZSONObject zsonObject = new ZSONObject();
        String contactPersonList = MyLocalParticleAbility.getInstance(context.getApplicationContext()).getContactPersonList();
        JSONArray jsonArray = JSON.parseArray(contactPersonList);
        for (int i = 0; i < jsonArray.size(); i++) {
            String name = jsonArray.getJSONObject(i).getString("item_name");
            String phone = jsonArray.getJSONObject(i).getString("item_phone");
            if (i == 0) {
                zsonObject.put("titleText", name);
                zsonObject.put("contentText", phone);
            } else if (i == 1) {
                zsonObject.put("titleText1", name);
                zsonObject.put("contentText1", phone);
            } else {
                break;
            }
            HiLog.info(TAG, "bind form data :" + jsonArray.getJSONObject(i).get("item_name"));
            HiLog.info(TAG, "bind form data :" + jsonArray.getJSONObject(i).get("item_phone"));
        }

        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        providerFormInfo.setJsBindingData(new FormBindingData(zsonObject));

        return providerFormInfo;
    }

    @Override
    public void updateFormData(long formId, Object... vars) {
        HiLog.info(TAG, "update form data timing, default 30 minutes");
    }

    @Override
    public void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "handle card click event." + message);

        ZSONObject zsonObject = ZSONObject.stringToZSON(message);

        // Do something here after receive the message from js card
        ZSONObject result = new ZSONObject();
        switch (zsonObject.getString("mAction")) {
            case "callEvent1":
                call(0);
                break;
            case "callEvent2":
                call(1);
                break;
        }
    }

    private void call(int index) {
        String contactPersonList = MyLocalParticleAbility.getInstance(context.getApplicationContext()).getContactPersonList();
        JSONArray jsonArray = JSON.parseArray(contactPersonList);
        if (jsonArray.size() > index) {
            String phone = jsonArray.getJSONObject(index).getString("item_phone");
            MyLocalParticleAbility.getInstance(context).doDial(phone);
        }
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        HiLog.info(TAG, "get the default page to route when you click card.");
        return null;
    }


}
